
import '../../../../core/app_export.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../add_gym_admin_page/bloc/add_admin_page_screen.dart';
import '../widgets/admin_item_view.dart';
import 'admins_cubit.dart';
import 'package:flutter/material.dart';

class GymsAdminsScreen extends StatelessWidget {
  const GymsAdminsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    var coachesCubit = AdminCubit();

    return Scaffold(
      appBar: _buildAppBar(context: context,cubit: coachesCubit),
      body: BlocProvider<AdminCubit>(
        create: (context) => coachesCubit..init(),
        child: BlocConsumer<AdminCubit, AdminsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = AdminCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: ScreenStateWidget(
                isDone: state is AdminsDone,
                isError: state is AdminsError,
                isLoading: state is AdminsLoading,
                onRefresh: cubit.getAllUsers,
                onDone: ListView.builder(
                    itemCount: cubit.users.length,
                    itemBuilder: (context,index){
                      return AdminItemView(user: cubit.users[index]);
                    }),
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({
    required BuildContext context ,
    required AdminCubit cubit}) {
    return CustomAppBar(
      actions: [
        BlocProvider<AdminCubit>(
          create: (context) => cubit,
          child: PrefUtils.isAdmin() ? OutlineIconButton(
            icon: Icons.add,
            margin:const EdgeInsets.all(8),
            padding:const EdgeInsets.all(2),
            onTapped: ()async{
              await Navigator.push(context, MaterialPageRoute(builder: (context) => const AddGymAdminPageScreen()));
            },
          ): null,
        )
      ],
      leading: OutlineIconButton(
        imagePath: ImageConstant.imgRightIcon,
        onTapped: (){
          Navigator.pop(context);
        },
      ),
      title: AppbarSubtitle(text: 'مُدِيرو النوادي'),
    );
  }
}
